package com.example;

import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.databinding.adapters.ViewBindingAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View;

import com.example.databinding.ActivityAttributesExampleBinding;


public class AttributesExampleActivity extends AppCompatActivity {

    private static final String TAG = "DataBinding";
    final ObservableField<String> imageUrl = new ObservableField<>();
    final ObservableInt paddingLeft = new ObservableInt();
    final ObservableField<View.OnLayoutChangeListener> onLayoutChangeListenerObservableField = new ObservableField<>();
    final ObservableField<ViewBindingAdapter.OnViewDetachedFromWindow> onViewDetachedFromWindowObservableField = new ObservableField<>();
    final ObservableField<ViewBindingAdapter.OnViewAttachedToWindow> onViewAttachedToWindowObservableField = new ObservableField<>();

    private View.OnLayoutChangeListener listener = new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            Log.d(TAG, "onLayoutChange: " + v);
        }
    };

    private ViewBindingAdapter.OnViewDetachedFromWindow onViewDetachedFromWindow = new ViewBindingAdapter.OnViewDetachedFromWindow() {
        @Override
        public void onViewDetachedFromWindow(View v) {
            Log.d(TAG, "onViewDetachedFromWindow: " + v);
        }
    };

    private ViewBindingAdapter.OnViewAttachedToWindow onViewAttachedToWindow = new ViewBindingAdapter.OnViewAttachedToWindow() {
        @Override
        public void onViewAttachedToWindow(View v) {
            Log.d(TAG, "onViewAttachedToWindow: " + v);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityAttributesExampleBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_attributes_example);
        imageUrl.set("http://img3.redocn.com/tupian/20150929/caiseyuanqiudongtailianjiePPT_4580490.jpg");
        binding.setImageUrl(imageUrl);
        paddingLeft.set(100);
        onLayoutChangeListenerObservableField.set(listener);
        onViewDetachedFromWindowObservableField.set(onViewDetachedFromWindow);
        onViewAttachedToWindowObservableField.set(onViewAttachedToWindow);
        binding.setListener(onLayoutChangeListenerObservableField);
        binding.setDetachListener(onViewDetachedFromWindowObservableField);
        binding.setAttachListener(onViewAttachedToWindowObservableField);
        binding.setPaddingLeft(paddingLeft);
    }
}
